using System;
using System.Text;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Web;

using Framework;

namespace SchemaMonitor
{
    //Collection Class (Customisable half)
    public partial class CChapterList
    {
		#region Filters
		#endregion

		#region Aggregation
		#endregion



		#region Members
		private CSectionList _sections;
		private CNodeList _nodes;

		private List<int> _sectionIds;
		private List<Guid> _nodeIds;
		#endregion


		#region Resolve Associative table (and sort)
		[Obsolete]
		public CNodeList Nodes
		{
			get
			{
				if (_nodes == null)
				{
					lock (this)
					{
						if (null == _nodes)
						{
							_nodes = new CNodeList(this.Count);
							foreach (CChapter i in this)
								if (i.Node != null)
									_nodes.Add(i.Node);
							_nodes.Sort();
						}
					}
				}
				return _nodes;
			}
		}
		public CSectionList Sections
		{
			get
			{
				if (_sections == null)
				{
					lock (this)
					{
						if (null == _sections)
						{
							_sections = new CSectionList(this.Count);
							foreach (CChapter i in this)
								if (i.Section != null)
									_sections.Add(i.Section);
							_sections.Sort();
						}
					}
				}
				return _sections;
			}
		}
		public CSectionList RemainingSections(string search)
		{
			CSectionList temp = new CSectionList(CSection.Cache.Search(search));
			temp.Remove(this.Sections);
			return temp;
		}
		#endregion

		#region Resolve/Isolate PKs
		public List<int> SectionIds
		{
			get
			{
				if (null == _sectionIds)
				{
					lock (this)
					{
						if (null == _sectionIds)
						{
							_sectionIds = new List<int>(this.Count);
							foreach (CChapter i in this)
								_sectionIds.Add(i.ChapterSectionId);
						}
					}
				}
				return _sectionIds;
			}
		}

		public List<Guid> NodeIds
		{
			get
			{
				if (null == _nodeIds)
				{
					lock (this)
					{
						if (null == _nodeIds)
						{
							_nodeIds = new List<Guid>(this.Count);
							foreach (CChapter i in this)
								_nodeIds.Add(i.ChapterNodeGuid);
						}
					}
				}
				return _nodeIds;
			}
		}
		#endregion


		#region Cloning
		public CChapterList Clone(CDataSrc target) //, int parentId)
        {
            //No Transaction
            if (target is CDataSrcRemote)
                return Clone(target, null); //, parentId);

            //Transaction
            using (IDbConnection cn = target.Local.Connection())
            {
                IDbTransaction tx = cn.BeginTransaction();
                try
                {
                    CChapterList clone = Clone(target, tx); //, parentId);
                    tx.Commit();
                    return clone;
                }
                catch
                {
                    tx.Rollback();
                    throw;
                }
            }
        }
        public CChapterList Clone(CDataSrc target, IDbTransaction txOrNull) //, int parentId)
        {
            CChapterList list = new CChapterList(this.Count);
            foreach (CChapter i in this)
                list.Add(i.Clone(target, txOrNull)); //, parentId));  *Child entities must reference the new parent
            return list;
        }
		#endregion


		//Logic

		public new void Add(CChapter item)
		{
			if (null != _index && !_index.ContainsKey(item.ChapterId))
				_index[item.ChapterId] = item;
			_indexByNodeGuid = null;
			_indexBySectionId = null;
			base.Add(item);
		}
		public new void Remove(CChapter item)
		{
			if (null != _index && _index.ContainsKey(item.ChapterId))
				_index.Remove(item.ChapterId);
			_indexByNodeGuid = null;
			_indexBySectionId = null;
			base.Remove(item);
		}

		protected void ExportToCsv(StreamWriter sw)
		{
			string[] headings = new string[] { "ChapterId", "ChapterSectionId", "ChapterNodeGuid", "ChapterName", "ChapterSortOrder", "ChapterText", "ChapterCreated" };
			CDataSrc.ExportToCsv(headings, sw);
			foreach (CChapter i in this)
			{
				object[] data = new object[] { i.ChapterId, i.ChapterSectionId, i.ChapterNodeGuid, i.ChapterName, i.ChapterSortOrder, i.ChapterText, i.ChapterCreated };
				CDataSrc.ExportToCsv(data, sw);
			}
		}
	}
}
